<template>
  <div class="app" @touchstart="watchTouchMove"  @touchmove="watchTouchMove">
    <div class="main">
      <p class="text">已有 <span>{{getNumber}}</span> 人参与悼念</p>
      <div class="btn" v-btn:click="link"></div>
    </div>
  </div>
</template>

<script lang="ts">
import { webCellManager, webCellReady, adjustWebViewInfo } from './libs/client';
import { openUrlByWebview } from '@tencent/gh-qqnews-sdk/gh-qqnews-jsapi';
import { ios, qqnews } from './libs/ua';
import beacon from './libs/reports/beacon';
/* 设置cell高度 */
function setCellHeight(scale: number) {
  adjustWebViewInfo({
    aspectRatio: scale,
    needFullWidth: 1,
  });
  // ios有个bug，需要2次设置cell高度
  if (ios) {
    setTimeout(() => {
      adjustWebViewInfo({
        aspectRatio: scale,
        needFullWidth: 1,
      });
    }, 200);
  }
}
</script>

<script setup lang="ts">
import { computed, onMounted, ref, getCurrentInstance, nextTick } from 'vue';
import { setNumber, getNumber } from './libs/numberTransition';
onMounted(() => {
  // 获取参与量（有过渡动画）
  setNumber(12345);
  if (qqnews) {
    // 腾讯新闻cell中调用此方法，需传入重置cell高度方法和日夜间模式方法
    webCellManager({
      resizeWindow: () => {
        setCellHeight(750 / 410);
      },
      changeTheme: () => {

      },
    });
  }
  nextTick(() => {
    setCellHeight(750 / 410);
    webCellReady();
    beacon.report('webcell_ready', {});
  });
});
/* 跳转链接 */
function link() {
  // 埋点
  beacon.report('webcell_btn', {});
  setTimeout(() => {
    if (qqnews) {
      // news 端内
      openUrlByWebview('https://view.inews.qq.com/hotEvent/UTR2021090300822100?ADTAG=webcell');
      // openUrlByWebview(`qqnews://article_9527?nm=UTR2021090300822100&from=webcell&_addLink=1&_addparams=${
      //   encodeURIComponent(JSON.stringify({
      //     // ...urlParams
      //   }))
      // }`);

      // if (window?.TencentNews?.openWebViewWithType) {
      //   // 调取透明webview
      //     window.TencentNews.openWebViewWithType('https://new.qq.com/qqfile/tnewsh5/midautum/poster.html?ADTAG=webcell', '100');
      // }
    } else {
      // 端外
      top!.window.location.href = 'https://view.inews.qq.com/a/ICT2021092900683900';
    }
  }, 200);
}
// 是否为卡片cell的滑动
const isTouchMove = ref(false);
const touchInit = {
  x: 0,
  y: 0,
};
// 监听滑动 并设置 isTouchMove 和上报埋点
function watchTouchMove(ev: TouchEvent) {
  switch (ev.type) {
    case 'touchstart': {
      isTouchMove.value = false;
      touchInit.x = ev.touches[0].pageX;
      touchInit.y = ev.touches[0].pageY;
      break;
    }
    case 'touchmove': {
      if (!isTouchMove.value) {
        if (2 * Math.abs(ev.touches[0].pageX - touchInit.x) > Math.abs(ev.touches[0].pageY - touchInit.y)) {
          isTouchMove.value = true;
          beacon.report('webcell_active', {
            type: 'touchmove',
          });
        }
      }
      break;
    }
  }
  if (isTouchMove.value) {
    ev.preventDefault();
  }
}
</script>


<style lang="postcss">
.app {
  position: absolute;
  top: 0;left: 0;
  width: 100%;height: 100%;
  color: #fff;
  .main{
    position: absolute;
    top: 0;
    left: 30px;
    width: 690px;
    height: 380px;
    background: #071b39 center no-repeat;
    background-size: 100% 100%;
    border-radius: 10px;
    overflow: hidden;
  }
}
.directive-btn{
  transition:transform .4s ease;
}
</style>
